package com.cait.cait.aop;

import com.cait.cait.controller.BasicController;
import com.cait.cait.resultMsgUtil.StatusMsgUtil;
import com.cait.cait.resultVo.ResultVo;
import lombok.NonNull;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author Jirath
 * @date 2020/2/12
 * @description:
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    StatusMsgUtil statusMsgUtil;
    private static final Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.cait.cait.controller.*.*(..)))")
    public void allController(){
    }

    @Around("allController()")
    public Object controllerAfterThrowing(ProceedingJoinPoint joinPoint) {
        Class clazz = joinPoint.getTarget().getClass();
        String clazzName = clazz.getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Object result = null;

        try {
            result = joinPoint.proceed();
            return result;
        } catch (Throwable var9) {
            StringWriter sw = new StringWriter();
            var9.printStackTrace(new PrintWriter(sw));
            log.error("**************************************************************");
            log.error("method info: " + this.methodInfo(clazzName, methodName, args));
            log.error("Exception class: " + var9.getClass().getName());
            log.error("Exception msg:" + sw.toString());
            log.error("**************************************************************");
            return ResultVo.builder().code(statusMsgUtil.getLockedAccount()).message("lockedAccount").data(null).build();
        }
    }
    public  String methodInfo(@NonNull String clazzName, @NonNull String methodName, @NonNull Object[] args) {
        if (clazzName == null) {
            throw new NullPointerException("clazzName");
        } else if (methodName == null) {
            throw new NullPointerException("methodName");
        } else if (args == null) {
            throw new NullPointerException("args");
        } else {
            StringBuffer sb = (new StringBuffer(clazzName)).append(".").append(methodName).append("(");
            boolean isFirst = true;
            Object[] var5 = args;
            int var6 = args.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                Object arg = var5[var7];
                if (isFirst) {
                    sb.append(arg);
                    isFirst = false;
                } else {
                    sb.append(", ").append(arg);
                }
            }

            sb.append(")");
            return sb.toString();
        }
    }
}